package com.dingreading.cloud.soms.util;

import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.EnumType;
import com.dingreading.cloud.common.util.KitUtil;
import com.dingreading.cloud.soms.dto.CaScheduleLessonDto;
import com.dingreading.cloud.soms.dto.CaTeacherDto;
import com.dingreading.cloud.soms.dto.CallHourDto;
import com.dingreading.cloud.soms.entity.CaCallNames;
import com.dingreading.cloud.soms.entity.CaSchedule;
import com.dingreading.cloud.soms.entity.CaScheduleLesson;
import com.dingreading.cloud.soms.entity.CaTeacher;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 排课工具
 */
public class ScheduleUtil {


    /**
     * 星期数据集合
     */
    public static final String[] WeekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    public static final String[] studentTypeNames = {"活动会员", "补缺会员", "插班会员", "试听会员"};
    public static final String[] callStatusNames = {"已到", "迟到", "请假", "未到"};

    // 体验课
    public static final String TIYAN_UID = "50439d23284947dc8fc98f8ea5d1318e";
    public static final String HAODU_UID = "8067fd50183f4df4b808acbf34942590";
    public static final String JINGDU_UID = "9ad8250840254982a34ab750ac8e2d87";


    /**
     * 创建点名记录
     */
    public static CaCallNames newCallName(
            CaCallNames data, CaScheduleLesson lesson, String corpUid, String storeUid, String storeName, String staffUid, String staffName,
            String teacherName, Long memberId, String memberUid, String memberName, String mob, Integer studentType,
            Integer callStatus, Integer makeupStatus, Integer status, Date callTime, String remark
    ) {
        if (null == data || null == data.getId() || data.getId() <= 0) {
            data = new CaCallNames();
            data.setCorpUid(corpUid);
            data.setStoreUid(storeUid);
            data.setStoreName(storeName);
            data.setUid(KitUtil.getUuid());
            data.setScheduleUid(lesson.getScheduleUid());
            data.setLessonUid(lesson.getLessonUid());
            data.setFreeStatus(lesson.getFreeStatus());
            data.setMemberId(memberId);
            data.setMemberUid(memberUid);
            data.setAddStaffUid(staffUid);
            data.setAddStaffName(staffName);
            if (null != studentType) {
                data.setStudentType(studentType);
                data.setStudentTypeName(studentTypeNames[studentType - 1]);
            } else {
                data.setStudentType(1);
                data.setStudentTypeName(studentTypeNames[0]);
            }
            if (null != callStatus) {
                data.setCallStatus(callStatus);
                if (!callStatus.equals(-1))
                    data.setCallStatusName(callStatusNames[callStatus - 1]);
                else
                    data.setCallStatusName("活动次数不足");
            }
            // 只记录第一次点名的时间
            if (null != callTime)
                data.setCallTime(callTime);
        } else {
            // 更新点名记录
            if (null != callStatus) {
                data.setCallStatus(callStatus);
                if (!callStatus.equals(-1))
                    data.setCallStatusName(callStatusNames[callStatus - 1]);
                else
                    data.setCallStatusName("活动次数不足");
            }
            // 只记录第一次点名的时间
            if (null == data.getCallTime() && null != callTime)
                data.setCallTime(callTime);
        }
        data.setStatus(status);
        data.setMakeupStatus(makeupStatus);
        data.setTeachDate(lesson.getTeachDate());
        data.setWeekNum(lesson.getWeekNum());
        data.setWeekName(lesson.getWeekName());
        data.setBeginTime(lesson.getBeginTime());
        data.setEndTime(lesson.getEndTime());
        data.setClassId(lesson.getClassId());
        data.setClassUid(lesson.getClassUid());
        data.setClassName(lesson.getClassName());
        data.setProjectUid(lesson.getProjectUid());
        data.setProductName(lesson.getProductName());
        data.setStageUid(lesson.getStageUid());
        data.setStageName(lesson.getStageName());
        data.setRoomId(lesson.getRoomId());
        data.setRoomUid(lesson.getRoomUid());
        data.setRoomName(lesson.getRoomName());
        data.setContent(lesson.getContent());
        data.setTeacherUid(lesson.getTeacherUid());
        if (StringUtils.isNotBlank(teacherName))
            data.setTeacherName(teacherName);
        else
            data.setTeacherName(lesson.getTeacherName());
        data.setUsernames(lesson.getUsernames());
        data.setMemberName(memberName);
        data.setMob(mob);
        data.setRemark(remark);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);
        return data;
    }

    /**
     * 为活动列表设置教师集合
     */
    public static void listSetTeachers(List<CaScheduleLessonDto> list, List<CaTeacherDto> teachers) {
        if (CollectionUtils.isNotEmpty(teachers)) {
            // 获取活动计划里的导读
            LinkedHashMap<String, List<CaTeacherDto>> scheduleTeacherMap = getScheduleTeacherMap(teachers);
            // 获取活动里的导读
            LinkedHashMap<String, List<CaTeacherDto>> lessonTeacherMap = getLessonTeacherMap(teachers);

            for (CaScheduleLessonDto lesson : list) {
                // 先获取活动的自定义主讲导读，如果没有则获取活动表的主讲导读
                List<CaTeacherDto> teacherList = lessonTeacherMap.get(lesson.getLessonUid());
                if (CollectionUtils.isEmpty(teacherList))
                    teacherList = scheduleTeacherMap.get(lesson.getScheduleUid());
                lesson.setTeacherList(teacherList);
            }
        }
    }

    /**
     * 为活动设置教师集合
     */
    public static void objSetTeachers(CaScheduleLessonDto lessonDto, List<CaTeacherDto> teachers) {
        if (CollectionUtils.isNotEmpty(teachers)) {
            // 获取活动计划里的导读
            LinkedHashMap<String, List<CaTeacherDto>> scheduleTeacherMap = getScheduleTeacherMap(teachers);
            // 获取活动里的导读
            LinkedHashMap<String, List<CaTeacherDto>> lessonTeacherMap = getLessonTeacherMap(teachers);

            // 先获取活动的自定义主讲导读，如果没有则获取活动表的主讲导读
            List<CaTeacherDto> teacherList = lessonTeacherMap.get(lessonDto.getLessonUid());
            if (CollectionUtils.isEmpty(teacherList))
                teacherList = scheduleTeacherMap.get(lessonDto.getScheduleUid());
            lessonDto.setTeacherList(teacherList);
        }
    }

    /**
     * 获取活动计划里的导读
     */
    private static LinkedHashMap<String, List<CaTeacherDto>> getScheduleTeacherMap(List<CaTeacherDto> teachers) {
        return teachers.stream()
                .filter(teacher -> StringUtils.isBlank(teacher.getLessonUid()))
                .collect(Collectors.groupingBy(CaTeacherDto::getScheduleUid, LinkedHashMap::new, Collectors.toList()));
    }

    /**
     * 获取活动自定义的导读
     */
    private static LinkedHashMap<String, List<CaTeacherDto>> getLessonTeacherMap(List<CaTeacherDto> teachers) {
        return teachers.stream()
                .filter(teacher -> StringUtils.isNotBlank(teacher.getLessonUid()))
                .collect(Collectors.groupingBy(CaTeacherDto::getLessonUid, LinkedHashMap::new, Collectors.toList()));
    }

    /**
     * 统计课时消耗数量
     */
    public static CallHourDto getHourType(Integer freeStatus, Integer callStatus, Integer studentType, String orderType, String serviceUid, Integer isGift) {
        CallHourDto dto = new CallHourDto();
        int hourType = -1;
        String hourTypeName = "未扣";
        if (freeStatus.equals(0)) {
            hourType = 0;
            hourTypeName = "免费";
        } else if (studentType.equals(4)) {
            // 4.试听会员
            hourType = 4;
            hourTypeName = "试听";
        } else {
            if (callStatus.equals(-1) || callStatus.equals(3) || callStatus.equals(4) || StringUtils.isBlank(orderType) || StringUtils.isBlank(serviceUid)) {
                // 未扣课时会员
                hourType = -1;
                hourTypeName = "未扣";
            } else {
                // 1.本活动会员 2.补缺会员 3.插班会员
                if (orderType.equals(EnumType.OrderType.EXP.getType())) {
                    // 体验会员 扣了课时
                    hourType = 3;
                    hourTypeName = "体验";
                } else if (orderType.equals(EnumType.OrderType.GIFT.getType())) {
                    // 赠送会员 扣了课时
                    hourType = 2;
                    hourTypeName = "赠送";
                } else {
                    // 正价会员 扣了课时
                    if (isGift != null && isGift.equals(0)) {
                        hourType = 1;
                        hourTypeName = "正价";
                    } else {
                        // 付费会员赠送
                        hourType = 2;
                        hourTypeName = "赠送";
                    }
                }
            }
        }

        dto.setHourType(hourType);
        dto.setHourTypeName(hourTypeName);
        return dto;
    }


    /**
     * 创建活动表的教师
     */
    public static void newTeachers(
            List<CaTeacher> teacherList, String corpUid, String storeUid, String scheduleUid, String lessonUid, String addUid, String addName
    ) {
        // 创建活动表的活动
        for (CaTeacher teacher : teacherList) {
            teacher.setCorpUid(corpUid);
            teacher.setStoreUid(storeUid);
            teacher.setScheduleId(null);
            teacher.setScheduleUid(scheduleUid);
            teacher.setLessonId(null);
            teacher.setLessonUid(lessonUid);
            teacher.setAddStaffUid(addUid);
            teacher.setAddStaffName(addName);
        }
    }

    /**
     * 创建活动
     */
    public static CaScheduleLesson newLesson(
            CaSchedule data, String date, int studentNum, String usernames, String teacherUid, String teacherName
    ) throws Exception {
        CaScheduleLesson lesson = new CaScheduleLesson();
        lesson.setLessonUid(KitUtil.getUuid());
        lesson.setCorpUid(data.getCorpUid());
        lesson.setStoreUid(data.getStoreUid());
        lesson.setStoreName(data.getStoreName());
        lesson.setScheduleUid(data.getScheduleUid());
        lesson.setTeachDate(date);
        int weekNum = DateUtil.getWeekNum(DateUtil.parseDate(date));
        lesson.setWeekNum(weekNum);
        lesson.setWeekName(WeekDays[weekNum]);
        lesson.setBeginTime(data.getBeginTime());
        lesson.setEndTime(data.getEndTime());
        lesson.setClassId(data.getClassId());
        lesson.setClassUid(data.getClassUid());
        lesson.setClassName(data.getClassName());
        lesson.setProjectUid(data.getProjectUid());
        lesson.setProductName(data.getProductName());
        lesson.setStageUid(data.getStageUid());
        lesson.setStageName(data.getStageName());
        lesson.setRoomId(data.getRoomId());
        lesson.setRoomUid(data.getRoomUid());
        lesson.setRoomName(data.getRoomName());
        lesson.setContent(data.getContent());
        lesson.setTeacherUid(teacherUid);
        lesson.setTeacherName(teacherName);
        lesson.setUsernames(usernames);
        lesson.setStudentNum(studentNum);
        lesson.setStudentTempNum(0);
        lesson.setReachNum(0);
        lesson.setLateNum(0);
        lesson.setAskLeaveNum(0);
        lesson.setNotReachNum(0);
        lesson.setNoHourNum(0);
        lesson.setRemark(null);
        lesson.setFreeStatus(data.getFreeStatus());
        lesson.setStatus(0);
        lesson.setAddStaffUid(data.getAddStaffUid());
        lesson.setAddStaffName(data.getAddStaffName());
        lesson.setLastStaffUid(data.getAddStaffUid());
        lesson.setLastStaffName(data.getAddStaffName());
        lesson.setCallTime(null);
        return lesson;
    }

    /**
     * 会员累计课时差额
     */
    public static Map<String, Table<String, String, Integer>> getLessonLoseNum() {
        Map<String, Table<String, String, Integer>> map = new HashMap<>();

        Table<String, String, Integer> table1 = HashBasedTable.create();
        table1.put("杨紫琪", "精读会", 14);
        table1.put("杨雨嘉", "精读会", 11);
        table1.put("梁宥琪", "精读会", 5);
        table1.put("费欣悦", "精读会", 3);
        table1.put("尹立宇", "精读会", 4);
        map.put("张丽君", table1);

        Table<String, String, Integer> table2 = HashBasedTable.create();
        table2.put("黄睿晨", "精读会", 14);
        table2.put("汪米诺", "精读会", 15);
        table2.put("赵弈涵", "精读会", 16);
        table2.put("王清湉", "精读会", 21);
        table2.put("潘汀晨", "精读会", 15);
        table2.put("李星玥", "精读会", 15);
        table2.put("李杨轩聆", "精读会", 7);
        table2.put("轩辕坤阳", "精读会", 14);
        table2.put("孔木子", "精读会", 2);
        table2.put("章玥琳", "精读会", 15);
        table2.put("曹陈子", "精读会", 8);
        table2.put("苏坦", "精读会", 13);
        table2.put("陈末", "精读会", 20);
        map.put("刘彤彤", table2);

        Table<String, String, Integer> table3 = HashBasedTable.create();
        table3.put("王靖豪2", "精读会", 14);
        table3.put("汪歆蕊", "精读会", 23);
        table3.put("高宏旭", "精读会", 14);
        table3.put("蔡雨欣", "精读会", 12);
        table3.put("王嘉禾", "精读会", 5);
        table3.put("葛天皛", "精读会", 14);
        table3.put("陈骏熙", "精读会", 26);
        table3.put("许召辰", "精读会", 11);
        table3.put("张可馨", "精读会", 12);
        table3.put("杜玗希", "精读会", 15);
        table3.put("杜玗涵", "精读会", 18);
        table3.put("郭婉瑜", "精读会", 14);
        map.put("陶军燕", table3);

        Table<String, String, Integer> table4 = HashBasedTable.create();
        table4.put("王楷瑞", "精读会", 13);
        table4.put("王世航", "精读会", 12);
        table4.put("李骏希", "精读会", 3);
        table4.put("花梓晗", "精读会", 2);
        table4.put("唐一鸣", "精读会", 6);
        table4.put("蒋雨溪", "精读会", 6);
        table4.put("姚柏宇", "精读会", 4);
        table4.put("周雅馨", "精读会", 3);
        table4.put("刘夏昕", "精读会", 8);
        map.put("朱爱玲", table4);

        Table<String, String, Integer> table5 = HashBasedTable.create();
        table5.put("朱欣怡", "文学社", 10);
        table5.put("方康晴", "文学社", 18);
        table5.put("张砚博", "文学社", 17);
        map.put("宫佳会", table5);

        Table<String, String, Integer> table6 = HashBasedTable.create();
        table6.put("耿楚皓", "文学社", 5);
        table6.put("余一鸣", "文学社", 60);
        table6.put("刘子乐", "文学社", 60);
        map.put("任雨梅", table6);

        Table<String, String, Integer> table7 = HashBasedTable.create();
        table7.put("胡振涵", "文学社", 21);
        table7.put("王晗清", "文学社", 18);
        table7.put("王梓轩", "文学社", 18);
        table7.put("马岩", "文学社", 18);
        table7.put("杨瀚辰", "文学社", 19);
        table7.put("方雅琪", "文学社", 18);
        table7.put("庞国周", "文学社", 18);
        table7.put("尹世祺", "文学社", 19);
        table7.put("朱甜睿", "文学社", 15);
        table7.put("鲁星亮", "文学社", 16);
        table7.put("尹立宇", "文学社", 16);
        map.put("王婉", table7);

        Table<String, String, Integer> table8 = HashBasedTable.create();
        table8.put("张赵帅", "文学社", 14);
        table8.put("吕思宇", "文学社", 18);
        table8.put("易宸羽", "文学社", 34);
        table8.put("王子睿", "文学社", 18);
        table8.put("胡铭涵", "精读会", 16);
        table8.put("李明浩", "精读会", 19);
        table8.put("易宸羽", "精读会", 10);
        table8.put("赵茗扬", "文学社", 8);
        table8.put("刘洪潮", "文学社", 9);
        table8.put("张庭瑞", "文学社", 52);
        table8.put("刘彦清", "文学社", 45);
        map.put("李静", table8);

        Table<String, String, Integer> table9 = HashBasedTable.create();
        table9.put("张子坤", "精读会", 20);
        table9.put("徐永程", "精读会", 21);
        table9.put("许雅睿", "精读会", 23);
        table9.put("余一鸣", "精读会", 17);
        table9.put("程康薇", "精读会", 15);
        table9.put("朱梓涵", "精读会", 15);
        table9.put("汪煜桐", "精读会", 16);
        table9.put("魏雨航", "精读会", 22);
        table9.put("张修齐", "精读会", 9);
        table9.put("李聪聪", "精读会", 17);
        table9.put("孙牧宸", "文学社", 12);
        table9.put("刘钰浠", "精读会", 16);
        table9.put("熊皓扬", "精读会", 17);
        table9.put("张智逸", "精读会", 20);
        table9.put("吴涵蕊", "精读会", 15);
        table9.put("郑语彤", "精读会", 6);
        table9.put("何思阳", "精读会", 10);
        table9.put("汤烁兮", "精读会", 12);
        map.put("沈悦悦", table9);

        Table<String, String, Integer> table10 = HashBasedTable.create();
        table10.put("朱子伊", "精读会", 16);
        table10.put("余传皓", "精读会", 3);
        table10.put("汪柏杨", "精读会", 6);
        map.put("化云娜", table10);

        return map;
    }

}
